package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Set;

/**
 * @program: health_paret
 * @description:
 * @author: Mr.Wang
 * @create: 2020-11-07 21:26
 **/
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    // 增加检查组信息
    @Override
    public Boolean saveSetmeal(SetmealDTO setmealDTO) {
        Integer id = setmealDTO.getId();
        System.out.println("================================" + id);
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            baseMapper.deletezhongjian(id);
        }
        boolean save = this.saveOrUpdate(setmealDTO);
        Integer sid = setmealDTO.getId();
        //然后将我们dto中绑定的‘检查项’给附上值
        System.out.println("sid = " + sid);
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        //Integer[] checkitemIds = setmealDTO.getCheckitemIds();
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer gid : checkgroupIds) {
                System.out.println("gid ===================== " + gid);
                baseMapper.addCheckGroupandSetmeal(sid, gid);
            }
        }
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET, setmealDTO.getImg());

        return save;
    }

    @Autowired
    private CheckGroupService checkGroupService;

    @Override
    public SetmealVO findCheckgroupInfoBySetmealId(int id) {
        int[] checkgroupIds = baseMapper.findCheckgroupid(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setCheckgroupIds(checkgroupIds);
        List<CheckGroup> list = checkGroupService.list();
        setmealVO.setCheckGroups(list);
        return setmealVO;
    }

    //删除缓存图片
    @Override
    public void clearRedis() {
        Set<String> membersOfSet = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if (membersOfSet.size() > 0 && membersOfSet != null) {
            for (String o : membersOfSet) {
                boolean b = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + o);
                if (b == false) {
                    //删除阿里云
                    AliyunUtils.deleteFile(o);
                    //删除set集合上的数据
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET, o);
                }
            }

        }
    }

    //  移动端进行套餐 套餐组以及检查项的查询和封装
    @Override
    public SetmealVO findSetMealDetail(int id) {
        SetmealVO setmeal = baseMapper.findSetmealByid(id);
        List<CheckGroupVO> findCheckGroup = baseMapper.findCheckGroupInfoSetmeal(id);
        for (CheckGroupVO checkGroupVO : findCheckGroup) {
            Integer checkGroupind = checkGroupVO.getId();
            List<CheckItem> checkItems = baseMapper.findCheckItemInfoCheckGroup(checkGroupind);
            checkGroupVO.setCheckItemList(checkItems);
        }
        setmeal.setCheckGroupVOS(findCheckGroup);
        return setmeal;
    }

    //
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//select * from t_user where setIs_delete = 0
        // 查看是否删除把0和1作为参数传进去
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<Setmeal>();
        //wrapper.eq("Is_delete", 0);
        //这里进行判断是有条件查询还是无条件查询的
        Page<Setmeal> page = null;
        if (!StringUtils.isBlank(queryPageBean.getQueryString())) {
            String queryString = queryPageBean.getQueryString();
            wrapper.like("helpcode", queryString).or().like("name", queryString).or().like("code", queryString);
            page = this.page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        } else {
            page = this.page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }

        List<Setmeal> records = page.getRecords();
        long total = page.getTotal();
        System.out.println("records = " + records);
        System.out.println("total = " + total);
        PageResult result = new PageResult(total, records);
        return result;
    }

    //// 进行逻辑上的删除
    //@Override
    //public Boolean updateCheckGroupById(int id) {
    //    CheckGroup checkGroup = new CheckGroup();
    //    checkGroup.setId(id);
    //    checkGroup.setIs_delete(1);
    //    boolean b = this.updateById(checkGroup);
    //    System.out.println("b = " + b);
    //    return b;
    //}
    //
    //// 这个的话主要是要回滚给前端的检查项
    //@Autowired
    //CheckItemService checkItemService;
    //
    //@Override
    //public CheckGroupVO findCheckItemInfoByGroupId(int id) {
    //    int[] checkitemInfoCheckGroup = baseMapper.findCheckitemInfoCheckGroup(id);
    //    List<CheckItem> list = checkItemService.list();
    //    CheckGroupVO checkGroupVO = new CheckGroupVO();
    //    checkGroupVO.setCheckItemList(list);
    //    checkGroupVO.setCheckitemIds(checkitemInfoCheckGroup);
    //    return checkGroupVO;
    //}
//
//    //进行添加检查项以及修改检查项
//    @Override
//    public Boolean add(CheckItem checkItem) {
//        //这个方法的话就是把增加和修改给整合了
//        boolean b = this.saveOrUpdate(checkItem);
//        return b;
//    }
}
